package com.tac.service.impl;

import com.tac.common.Exception.CostomException;
import com.tac.common.result.Result;
import com.tac.common.thread.Thread;
import com.tac.dao.CartDao;
import com.tac.dao.ProductDao;
import com.tac.pojo.Cart;
import com.tac.pojo.Product;
import com.tac.pojo.vo.Change;
import com.tac.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartDao cartDao;
    @Autowired
    private ProductDao productDao;
    /**
     * 添加
     * @return
     */
    @Override
    public Result inserte(Integer pid) {
        //判断库存够不够
        Product product = productDao.selectById(pid);
        if(product.getStock()<=0){
            throw new CostomException("库存不足");
        }
        //先判断有没有已经添加过
        Cart cart1 = cartDao.selectByPId(pid, Thread.getId());
        if(cart1!=null){
            cart1.setQuantity(cart1.getQuantity()+1);
            cart1.setTotalPrice(cart1.getTotalPrice().add(cart1.getPrice()));
            cartDao.updateById(cart1);
            return Result.success(null,"添加成功");
        }

        Cart cart = new Cart();
        cart.setPrice(product.getPrice());
        cart.setProductId(product.getId());
        cart.setProductImg(product.getMainImage());
        cart.setQuantity(1);
        cart.setProductName(product.getName());
        cart.setStatus(0);
        cart.setTotalPrice(product.getPrice());
        cart.setUserId(Thread.getId());
        Integer inserte = cartDao.inserte(cart);
        if(inserte>0){
            return Result.success(null,"添加成功");
        }
        return Result.error("添加失败");
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    public Result deleteById(Integer id){
        Cart cart = cartDao.selectById(id);
        if(cart.getUserId()!=Thread.getId()){
            return Result.error("不能删除别人的购物车");
        }
        Integer integer = cartDao.deleteById(id);
        if(integer>0){
            return Result.success(null,"success");
        }
        return Result.error("删除失败");
    }

    /**
     * 修改数量及价格
     * @param id
     * @param change
     * @return
     */
    public Result updateById(Integer id, Change change){
        Cart cart = cartDao.selectById(id);
        Integer quantity = cart.getQuantity();
        if(change.getStatus()==0){
            //加
            //判断库存够不够
            Product product = productDao.selectById(cart.getProductId());

            quantity=quantity+change.getQuantity();
            if(product.getStock()-quantity<=0){
                throw new CostomException("库存不足");
            }
        }else {
            //减
            if(quantity-change.getQuantity()>0){
                quantity=quantity-change.getQuantity();
            }else {
                return Result.error("不能再减了再减就没了");
            }

        }
        cart.setQuantity(quantity);//设置总数量
        //设置总价格
        BigDecimal price = cart.getPrice();
        cart.setTotalPrice(price.multiply(new BigDecimal(quantity)));
        Integer integer = cartDao.updateById(cart);
        if(integer>0){
            return Result.success(null,"success");
        }
        return Result.error("修改错误");
    }



    /**
     * 选中
     * @param id
     * @return
     */
    public Result updateByIdAndStatus(Integer id,Integer status){
        cartDao.updateByIdAndStatus(id,status);
        return Result.success(null,"success");
    }

    /**
     * 全部选中
     * @param status
     * @return
     */
    public Result updateByIdAndStatusAll( Integer status){
        cartDao.updateByIdAndStatusAll(Thread.getId(),status);
        return Result.success(null,"success");
    }
    /**
     * 根据用户id查看购物车
     * @param
     * @return
     */
    public Result<List<Cart>> selectByUid(){
        List<Cart> carts = cartDao.selectByUid(Thread.getId());
        return Result.success(carts,null);
    }


}
